<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="会场名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入会场名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" />
      <el-table-column label="排序" prop="sort" :show-overflow-tooltip="true" />
      <el-table-column label="名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="直播间地址" prop="liveUrl" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleStartLive(scope.row)" :disabled="scope.row.pushUrl">开启直播</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" append-to-body :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form" label-width="90px" style="height: 500px; overflow: auto">
        <el-form-item label="会场名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入会场名称" clearable />
        </el-form-item>
        <el-form-item label="会议地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入会议地点" clearable />
        </el-form-item>
        <el-form-item label="直播间地址" prop="liveUrl">
          <el-input v-model="form.liveUrl" placeholder="请输入直播间地址" clearable />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" :max="1000" style="width: 370px"></el-input-number>
        </el-form-item>
        <el-form-item label="企业专题会" prop="isCorporateTheme">
          <el-radio-group v-model="form.isCorporateTheme">
            <el-radio :label="'1'">是</el-radio>
            <el-radio :label="'0'">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="dialogLiveVisible" width="500px" append-to-body :close-on-click-modal="false">
      <el-form :model="formLive" :rules="rulesLive" ref="formLive" label-width="110px" style="height: 500px; overflow: auto">
        <el-form-item label="广告图片" prop="adImgUrl">
          <el-upload
            :action="uploadImgUrl"
            list-type="picture-card"
            :multiple="false"
            :limit="1"
            :headers="headers"
            :file-list="fileList"
            :before-upload="beforeUpload"
            :on-success="handleUploadSuccess"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="会议背景图片" prop="backgroundImg">
          <el-upload
            :action="uploadImgUrl"
            list-type="picture-card"
            :multiple="false"
            :limit="1"
            :headers="headers"
            :file-list="fileList2"
            :before-upload="beforeUpload2"
            :on-success="handleUploadSuccess2"
            :on-remove="handleRemove2">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitLiveForm">确 定</el-button>
        <el-button @click="cancelLive">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {listPlace, addPlace, updatePlace, getPlace, delPlace, startLive} from "@/api/meetingManagement/place";
import {getToken} from "@/utils/auth";

export default {
  name: "VenueManagement",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        meetId: undefined,
        roleKey: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        meetId: null,
        name: null,
        location: null,
        liveUrl: null,
        sort: null,
        isCorporateTheme: null
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ]
      },
      dialogVisible: false,
      dialogLiveVisible: false,
      formLive: {
        adImgUrl: null,
        backgroundImg: null,
        placeId: null
      },
      rulesLive: {
        adImgUrl: [
          { required: true, message: "广告图片不能为空", trigger: "blur" }
        ],
        backgroundImg: [
          { required: true, message: "会议背景图片不能为空", trigger: "blur" }
        ]
      },
      headers: {
        Authorization: "Bearer " + getToken(),
      },
       fileList: [],
       fileList2: [],
      uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
    }
  },
  mounted() {
    this.queryParams.meetId = this.$route.params && this.$route.params.id
    this.getList()
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true
      listPlace(this.addDateRange(this.queryParams)).then(response => {
          this.roleList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        meetId: null,
        name: null,
        location: null,
        liveUrl: null,
        sort: null,
        isCorporateTheme: null
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.dialogVisible = true
      this.title = "新增会场"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      console.log(id)
      getPlace(id).then(response => {
        this.form = response.data
        this.dialogVisible = true
      })
      this.title = "修改会场"
    },
    /** 分配用户操作 */
    handleAuthUser: function(row) {
      const roleId = row.roleId
      this.$router.push("/system/role-auth/user/" + roleId)
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.meetId = this.queryParams.meetId
          if (this.form.id != undefined) {
            updatePlace(this.form).then(response => {
              this.$modal.msgSuccess(response.msg || "修改成功")
              this.dialogVisible = false
              this.getList()
            })
          } else {
            addPlace(this.form).then(response => {
              this.$modal.msgSuccess(response.msg || "新增成功")
              this.dialogVisible = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除会场编号为"' + id + '"的数据项？').then(function() {
        return delPlace(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    handleStartLive(row) {
      const id = row.id
      this.dialogLiveVisible = true
      this.formLive = {
        adImgUrl: null,
        backgroundImg: null,
        placeId: id
      }
      // this.formLive = {row}
    },
    beforeUpload(file) {
      let imageUrl = URL.createObjectURL(file);
      const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error('上传图片只能是 png/JPG/gif 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 3MB!');
      }
      return isJPG && isLt2M;
    },
    handleRemove() {
      this.formLive.adImgUrl = null
      this.fileList = []

    },
    handleUploadSuccess(res, file) {
      if (res && res.data && res.data.path) {
        this.formLive.adImgUrl = res.data.path;
        this.fileList = [{ name: file.name, url: res.data.path }]
      } else if (res && res.url) {
        this.formLive.adImgUrl = res.url;
        this.fileList = [{ name: file.name, url: res.url }]
      }
    },
    beforeUpload2(file) {
      let imageUrl = URL.createObjectURL(file);
      const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error('上传图片只能是 png/JPG/gif 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 3MB!');
      }
      return isJPG && isLt2M;
    },
    handleRemove2() {
      this.formLive.backgroundImg = null
      this.fileList2 = []
    },
    handleUploadSuccess2(res, file) {
      if (res && res.data && res.data.path) {
        this.formLive.backgroundImg = res.data.path;
        this.fileList2 = [{ name: file.name, url: res.data.path }]
      } else if (res && res.url) {
        this.formLive.backgroundImg = res.url;
        this.fileList2 = [{ name: file.name, url: res.url }]
      }
    },
    submitLiveForm: function() {
      this.$refs["formLive"].validate(valid => {
        if (valid) {
          console.log(this.formLive)
          this.formLive.meetId = this.queryParams.meetId
          startLive(this.formLive).then(response => {
            this.$modal.msgSuccess(response.msg || "开启成功")
            this.cancelLive()
            this.getList()
          })
        }
      })
    },
    cancelLive: function() {
      this.dialogLiveVisible = false
      this.resetLiveForm()
    },
    resetLiveForm: function() {
      this.formLive = {
        adImgUrl: null,
        backgroundImg: null,
        placeId: null
      }
      this.fileList = []
      this.fileList2 = []
    }
  }
}
</script>
<style scoped lang="scss">

</style>
